#include "mainclass.h"
#include "ui_mainclass.h"
#include <QtCore/QCoreApplication>
#include "Building.h"
#include "QPropertyAnimation"

Building::Building(QPixmap* imgOn, QPixmap* imgOff, QString nm, QString setDesc, bool red, int slot, void *eng, Ui::MainClass* u) {
    isRed = red;
    description = setDesc;
    engine = eng;
    ui = u;
    name = nm;
    level = 1;
    commandTimer = new QTimer();
    commandTimer->start(1);

    if(isRed) {
        switch(slot) {
            case 1:
                displayLabel = ui->redSlot1;
                displayButton = ui->redSel1;
                break;
            case 2:
                displayLabel = ui->redSlot2;
                displayButton = ui->redSel2;
                break;
            case 3:
                displayLabel = ui->redSlot3;
                displayButton = ui->redSel3;
                break;
            case 4:
                displayLabel = ui->redSlot4;
                displayButton = ui->redSel4;
                break;
            case 5:
                displayLabel = ui->redSlot5;
                displayButton = ui->redSel5;
                break;
            case 6:
                displayLabel = ui->redSlot6;
                displayButton = ui->redSel6;
                break;
            case 7:
                displayLabel = ui->redSlot7;
                displayButton = ui->redSel7;
                break;
            case 8:
                displayLabel = ui->redSlot8;
                displayButton = ui->redSel8;
                break;
            default:
                break;
        }
    }
    else {
        switch(slot) {
            case 1:
                displayLabel = ui->blueSlot1;
                displayButton = ui->blueSel1;
                break;
            case 2:
                displayLabel = ui->blueSlot2;
                displayButton = ui->blueSel2;
                break;
            case 3:
                displayLabel = ui->blueSlot3;
                displayButton = ui->blueSel3;
                break;
            case 4:
                displayLabel = ui->blueSlot4;
                displayButton = ui->blueSel4;
                break;
            case 5:
                displayLabel = ui->blueSlot5;
                displayButton = ui->blueSel5;
                break;
            case 6:
                displayLabel = ui->blueSlot6;
                displayButton = ui->blueSel6;
                break;
            case 7:
                displayLabel = ui->blueSlot7;
                displayButton = ui->blueSel7;
                break;
            case 8:
                displayLabel = ui->blueSlot8;
                displayButton = ui->blueSel8;
                break;
            default:
                break;
        }

    }

    //displayButton->setGeometry();

    imageOn = imgOn;
    imageOff = imgOff;

    displayLabel->setPixmap(*imageOff);

    displayLabel->update();

    displayButton->setStyleSheet("border-color: rgba(255, 255, 255, 0);\nbackground-color: rgba(255, 255, 255, 0);");
    displayButton->setFocusPolicy(Qt::NoFocus);

    cmdList = new QList <Command*> ();
}

void Building::setHitpoints(int val) {
    hitpoints = val;
}

void Building::addCmd(QIcon* imgOn, QIcon* imgOff, QString name, QString desc, QString type, bool red) {
    Command* newCmd = new Command(imgOn,imgOff,name,desc,type,engine);
    if(isRed)
        newCmd->setParent(ui->leftBaseArea);
    else
        newCmd->setParent(ui->rightBaseArea);

    cmdList->append(newCmd);
}

void Building::select() {
    if(isRed)
        ((BattleEngine*)engine)->deselectAllRedBuildings();
    else
        ((BattleEngine*)engine)->deselectAllBlueBuildings();
    isSelected = true;
    displayLabel->setPixmap(*imageOn);
    displayCommands();
}

void Building::deselect() {
    isSelected = false;
    displayLabel->setPixmap(*imageOff);
}

int Building::getLevel() {
    return level;
}

QString Building::getName() {
    return name;
}

void Building::displayCommands() {
    QPoint* location;
    QPoint* startPt;
    int v = 0;
    if(cmdList->size() > 0)
        for(int i = 0 ; i < 360 ; i += 360 / cmdList->size()) {
            location = ((BattleEngine*)engine)->vectorXY(80,i + 90,displayButton->x() + 45,displayButton->y() + 45);
            if(cmdList->value(v) != NULL) {
                cmdList->value(v)->setGeometry(displayButton->x()+45-25, displayButton->y()+45-25, 50, 50);
                cmdList->value(v)->show();
                startPt = new QPoint(displayButton->x()+45-25, displayButton->y()+45-25);
                location->setX(location->x() - 25);
                location->setY(location->y() - 25);
                cmdList->value(v)->setPoints(startPt,location);
                cmdList->value(v)->moveToEndPoint(commandTimer);
            }

            v++;
        }

}

void Building::hideCommands() {
    for(int i = 0 ; i < cmdList->size() ; i ++) {
        cmdList->value(i)->hide();
        cmdList->value(i)->setGeometry(QRect(displayButton->x() + 45, displayButton->y() + 45, 90, 90));
    }
}

void Building::promote() {
    //clear commands list
    //reconstruct commands list
    if(!(QString::compare(name,"Barracks"))) {
        switch(level) {
            case 1:
                level++;
                //change sprites
                break;
            case 2:
                level++;

                break;
            case 3:

                //doesn't level up
                break;
            default:
                break; //invalid level
        }
    }
    else if(!(QString::compare(name,"Warfactory"))) {
        switch(level) {
            case 1:
                level++;
                break;
            case 2:
                level++;
                break;
            case 3:
                break;
            default:
                break; //invalid level
        }
    }
    else if(!(QString::compare(name,"Airfield"))) {
        switch(level) {
            case 1:
                level++;
                break;
            case 2:
                level++;
                break;
            case 3:
                break;
            default:
                break; //invalid level
        }
    }
    else if(!(QString::compare(name,"EconomicC"))) {
        switch(level) {
            case 1:
                level++;
                break;
            case 2:
                level++;
                break;
            case 3:
                break;
            default:
                break; //invalid level
        }
    }
    else if(!(QString::compare(name,"TechnologyC"))) {
        switch(level) {
            case 1:
                level++;
                break;
            case 2:
                level++;
                break;
            case 3:
                level++;
                break;
            case 4:
                break;
            default:
                break; //invalid level
        }
    }
    else if(!(QString::compare(name,"CommandCenter"))) {
        switch(level) {
            case 1:
                level++;
                break;
            case 2:
                level++;
                break;
            case 3:
                break;
            default:
                break; //invalid level
        }
    }

}

QPushButton* Building::getDiplayButton() {
    return displayButton;
}

Building::~Building() {
    delete this;
}
